package com.huilian.iotbox.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.dao.DeviceAnnualFeeDao;
import com.huilian.iotbox.admin.service.DeviceAnnualFeeService;
import com.huilian.iotbox.data.dto.DeviceAnnualFeeDto;
import com.huilian.iotbox.data.vo.DeviceAnnualFeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LinYinDong
 * @date 2024/7/17 下午5:01
 */
@Service
public class DeviceAnnualFeeServiceImpl implements DeviceAnnualFeeService {

    @Autowired
    private DeviceAnnualFeeDao deviceAnnualFeeDao;
    @Override
    public DeviceAnnualFeeVo getOneDeviceAnnualFeeByDeviceId(Integer deviceId, Integer userId, Integer merchantStoreId) {
        return deviceAnnualFeeDao.getOneDeviceAnnualFeeByDeviceId(deviceId, userId, merchantStoreId);
    }

    @Override
    public Integer addDeviceAnnualFee(DeviceAnnualFeeDto dto) {
        return deviceAnnualFeeDao.insert(dto);
    }

    @Override
    public PageInfo findDeviceAnnualFeeList(DeviceAnnualFeeDto deviceAnnualFeeDto) {
        PageHelper.startPage(deviceAnnualFeeDto.getPageNum(), deviceAnnualFeeDto.getPageSize(), false);
        PageHelper.orderBy("daf.dateOfFirstUse");
        List<DeviceAnnualFeeVo> orderList = deviceAnnualFeeDao.findDeviceAnnualFeeList(deviceAnnualFeeDto);
        PageInfo pagResult = new PageInfo(orderList);
        if (orderList.size() < 10) {
            pagResult.setTotal(deviceAnnualFeeDto.getPageSize() * deviceAnnualFeeDto.getPageNum());
        } else {
            pagResult.setTotal(deviceAnnualFeeDto.getPageSize() * deviceAnnualFeeDto.getPageNum() + 1);
        }
        return pagResult;
    }

    @Override
    public Integer delete(Integer id) {
        return deviceAnnualFeeDao.delete(id);
    }
}
